package com.tea.controller;

import com.tea.Model.Order;
import com.tea.Model.dto.OrderAddDTO;
import com.tea.Model.dto.ShipOrderDTO;
import com.tea.Model.dto.ShipStatusOnOrOffDTO;
import com.tea.Model.dto.TrackInfoDTO;
import com.tea.service.OrderService;
import com.tea.service.WxShippingService;
import com.tea.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;


@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Resource
    private OrderService orderService;
    
    @Resource
    private WxShippingService wxShippingService;

    /**
     * 创建订单
     * */
    @PostMapping("/create")
    public Result<?> createOrder(@RequestBody OrderAddDTO request) {
        try {
            log.info("创建订单请求: method={}, items={}, storeId={}, remarkPresent={}",
                    request.getShippingMethod(),
                    request.getSpecWithNum() != null ? request.getSpecWithNum().size() : 0,
                    request.getStoreId(),
                    request.getRemark() != null && !request.getRemark().isEmpty());
        } catch (Exception ignore) { }
        return orderService.createOrder(request);
    }

    /**
     * 支付回调
     */
    @PostMapping("/notify")
    public Result<?> wxPayCallback(HttpServletRequest request) throws IOException {
        String ip = request.getRemoteAddr();
        log.info("接收微信支付回调，来源IP={}", ip);
        orderService.wxPayCallback(request);
        return Result.success();
    }

    /**
     * 订单列表查询
     * */
    @GetMapping("/list")
    public Result<?> list(String status) {
        return orderService.orderList(status);
    }

    /**
     *
     * 获取全部订单
     * */
    @GetMapping("/all")
    public Result<?> list() {
        return orderService.orderAll();
    }


    /**
     * 订单详情
     * */
    @GetMapping("/{orderNo}")
    public Result<?> getOrderById(@PathVariable String orderNo) {
        return orderService.getOrder(orderNo);
    }

    /**
    * 获取配送费
    * */
    @GetMapping("/shippingFee")
    public Result<?> getShippingFee(@RequestParam String postcode ,String orderPrice){
        return orderService.getShippingFee(postcode,orderPrice);
    }

    /**
     * 确认收货
     * */
    @PostMapping("/confirm/{orderNo}")
    public Result<?> orderCompletion(@PathVariable String orderNo) {
        log.info("确认收货，orderNo={}", orderNo);
        return orderService.orderCompletion(orderNo);
    }

    /**
     * 物流详情
     * */
    @PostMapping("/track")
    public Object orderTrack(@RequestBody TrackInfoDTO trackInfoDTO) throws IOException {
        try { log.info("查询物流详情，请求单号={}", trackInfoDTO.getExpressNum()); } catch (Exception ignore) {}
        return orderService.orderTrack(trackInfoDTO);
    }

    /**
     * 订单更新
     * */
    @PutMapping("/update")
    public Result<?> updateOrder(@RequestBody Order order){
        try { log.info("更新订单，orderNo={}, status={}, expressNum={}", order.getOrderNo(), order.getStatus(), order.getExpressNum()); } catch (Exception ignore) {}
        return orderService.updateOrder(order);
    }

    /**
     * 订单发货
     */
    @PostMapping("/ship")
    public Result<?> shipOrder(@RequestBody @Validated ShipOrderDTO shipOrderDTO) {
        log.info("订单发货，orderNo={}, expressNum={}", shipOrderDTO.getOrderNo(), shipOrderDTO.getExpressNum());
        return orderService.shipOrder(shipOrderDTO);
    }
    
    /**
     * 查询小程序是否开通发货信息管理服务
     */
    @PostMapping("/is-trade-managed")
    public Result<?> isTradeManaged(@RequestBody ShipStatusOnOrOffDTO request) {
        String appid = request.getAppid();
        log.info("查询是否开通发货信息管理服务，appid={}", appid);
        return wxShippingService.isTradeManaged(appid);
    }
}